<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-07-30 20:02:30
 * @LastEditTime: 2020-02-28 11:25:38
 * @LastEditors: Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../css/base.css" />
    <script type="text/javascript" src="../script/jquery.js"></script>
    <script type="text/javascript" src="../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../script/vue.min.js"></script>
    <script type="text/javascript" src="../script/vant.min.js"></script>
    <script type="text/javascript" src="../script/fastclick.js"></script>
    <script type="text/javascript" src="../script/base.js"></script>
    <style>
        .scanPop {
            width: 160px;
            height: 180px;
            text-align: center;
            background-color: transparent;
            margin: 0;
            padding: 0;
        }

        .van-icon-enlarge {
            font-size: 150px;
            color: #e6c678;
            margin: 0;
            padding: 0;
        }

        .inputClass {
            opacity: 0;
            width: 150px;
        }

        .scanLine {
            position: absolute;
            z-index: 2;
            width: 66%;
            height: 2px;
            transform: translateX(25%);
            background-color: #e6c678;
            animation: scanLineMove 2s infinite;
        }

        .newClass {
            color: red;
        }

        @keyframes scanLineMove {
            from {
                top: 30px;
            }

            to {
                top: 120px;
            }
        }

        .box>.van-button {
            position: absolute;
            right: 20px;
            z-index: 1;
        }

        .content .table {
            padding-bottom: 20px;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="box">
            <van-button v-if="!TY" color="linear-gradient(#fffeaa, #c07d39)" icon="scan" type="primary" size="small"
                @click="openScan">
                扫描注塑箱码</van-button>
            <van-tabs type="card" v-model="activeTab" @change="tabClick()">
                <div v-for="item in vanTabs" v-if="item.show">
                    <van-tab :title="item.title">
                        <div class="content" id="content">
                            <div class="table">
                                <table class="table-full">
                                    <tr>
                                        <th nowrap="nowrap">能否报工</th>
                                        <th nowrap="nowrap">注塑计划</th>
                                        <th nowrap="nowrap">制单时间</th>
                                        <th nowrap="nowrap">生产单号</th>
                                        <th nowrap="nowrap">产品编码</th>
                                        <th nowrap="nowrap">产品名称</th>
                                        <th nowrap="nowrap">产品规格</th>
                                        <th nowrap="nowrap">计划数量</th>
                                        <th nowrap="nowrap">包装数量</th>
                                        <th nowrap="nowrap">完成数量</th>
                                        <th nowrap="nowrap">已报工数量</th>
                                        <th nowrap="nowrap">订单欠数</th>
                                        <th nowrap="nowrap">是否换模</th>
                                        <th nowrap="nowrap" v-if="TY">是否上机</th>
                                        <th nowrap="nowrap">是否已上模</th>
                                        <th nowrap="nowrap">注塑ID</th>
                                    </tr>

                                    <tbody>
                                        <tr v-for="item in tableData" @click="planIDClick(item)">
                                            <td nowrap="nowrap">{{item.ISRepor}}</td>
                                            <td nowrap="nowrap">{{item.ZhuSuPlanID}}</td>
                                            <td nowrap="nowrap">{{item.Billdate.split('T')[0]}}</td>
                                            <td nowrap="nowrap">{{item.ProductionNO}}</td>
                                            <td nowrap="nowrap">{{item.ProductCode}}</td>
                                            <td nowrap="nowrap">{{item.ProductName}}</td>
                                            <td nowrap="nowrap">{{item.ProductSpec}}</td>
                                            <td nowrap="nowrap">{{item.PlanQTY}}</td>
                                            <td nowrap="nowrap">{{item.BaoZhangQTY}}</td>
                                            <td nowrap="nowrap">{{item.CompletedQTY}}</td>
                                            <td nowrap="nowrap">{{item.ReporQTY}}</td>
                                            <td nowrap="nowrap">{{item.qianQTY}}</td>
                                            <td nowrap="nowrap">{{item.WhetherChangeMold}}</td>
                                            <td nowrap="nowrap" v-if="TY">{{item.WhetherStarteEquipment}}</td>
                                            <td nowrap="nowrap">{{item.WhetherStarteMold}}</td>
                                            <td nowrap="nowrap">{{item.ZS_ZhuSuReporID}}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </van-tab>
                </div>
            </van-tabs>

            <van-popup v-model="scanShow" class="scanPop">
                <div class="scanLine"></div>
                <van-icon name="enlarge"></van-icon>
                <input id="autoInput" inputmode="none" class="inputClass" type="text" v-model="inputCode" ref="input"
                    autofocus @oninput="inputFocus()">
            </van-popup>

            <van-dialog v-model="dialogShow" title="提示" show-cancel-button @confirm="handlerDialog"
                @cancel="cancelDialog">
                {{dialogMsg}}
            </van-dialog>
        </div>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        var scanS60A = api.require('ScanS60A');

        api.addEventListener({
            name: 'newTYcode'
        }, function (ret) {
            if (ret.value.TYcode) {
                that.activeTab = 0
                vm.tabClick()
            }
        })

        api.addEventListener({
            name: 'refreshChoosePlan'
        }, function (ret) {
            api.pageParam.choosePlan = false
            api.pageParam.equipment = JSON.parse(window.localStorage.getItem('Equipment'))
            // console.log('equipment', JSON.stringify(api.pageParam.equipment))
            vm.TY = api.pageParam.equipment.EquipmentName.indexOf('涂油') == -1 //不是涂油
            vm.inputCode = ''

            if (api.pageParam.equipment.EquipmentName.indexOf('涂油') == -1) { //普通注塑机
                vm.tabClick()
                vm.scanShow = false
            } else {
                vm.vanTabs[0].show = false
                vm.scanShow = true

                setInterval(() => {
                    vm.$refs.input.focus()
                    vm.$refs.input.setAttribute('readonly', 'readonly');
                    setTimeout(function () {
                        vm.$refs.input.removeAttribute('readonly');
                    }, 500);
                }, 1500)
            }
        })

        vm = new Vue({
            el: '#app',
            data: {
                activeTab: 0,
                tableData: [],
                vanTabs: [
                    {
                        title: '生产中',
                        show: true
                    },
                    {
                        title: '可报工',
                        show: true
                    },
                    {
                        title: '不可报工',
                        show: true
                    },
                    {
                        title: '已结束',
                        show: true
                    },
                ],
                TY: api.pageParam.equipment.EquipmentName.indexOf('涂油') == -1, //不是涂油
                scanShow: false,
                inputCode: '',
                beforeMsg: {},
                dialogShow: false,
                dialogMsg: ''
            },
            created: function () {
                var that = this;
                console.log('chooseAPI', JSON.stringify(api.pageParam))

                if (window.localStorage.getItem('clientHeight') != 752) {
                    document.getElementById('app').style.zoom = 0
                    document.getElementById('content').style.height = '85vh'
                } else {
                    document.getElementById('app').style.zoom = 1.5
                    document.getElementById('content').style.height = '65vh'
                }
                if (api.pageParam.equipment.EquipmentName.indexOf('涂油') == -1) { //普通注塑机
                    that.scanShow = false
                    that.tabClick()
                } else {
                    that.vanTabs[0].show = false
                    that.scanShow = true

                    setTimeout(() => {
                        that.$refs.input.setAttribute('readonly', 'readonly');
                    }, 500)
                    scanS60A.registerReceiver(function (ret, err) {
                        // alert('ret' + JSON.stringify(ret))
                        that.inputCode = ret.barcode
                        vm.$toast(ret.barcode);
                        that.tabClick()
                    })
                }
            },
            methods: {
                handlerDialog() {
                    var that = this
                    that.dialogShow = false
                    APP.ajax('updeteTYDY', { FBoxCode: that.inputCode }, function (res) {
                        that.tabClick()
                    })
                    // scanS60A.registerReceiver(function (ret, err) {
                    //     // alert('ret' + JSON.stringify(ret))
                    //     that.inputCode = ret.barcode
                    //     vm.$toast(ret.barcode);

                    // })
                },
                cancelDialog() {
                    var that = this
                    that.dialogShow = false
                    that.openScan()
                },
                inputFocus() {
                    var that = this
                    if (that.inputCode && that.inputCode.length == 24) {
                        vm.$toast(that.inputCode);
                        that.tabClick()
                    } else {
                        api.toast({
                            msg: '请输入正确的箱码！',
                            location: 'middle'
                        });
                        that.inputCode = ''
                    }
                },
                openScan() {
                    var that = this
                    that.inputCode = ''
                    that.scanShow = true
                    that.$refs.input.setAttribute('readonly', 'readonly');

                    scanS60A.registerReceiver(function (ret, err) {
                        // alert('ret' + JSON.stringify(ret))
                        that.inputCode = ret.barcode
                        vm.$toast(ret.barcode);
                        that.tabClick()
                    })
                },
                historyBack() {
                    var that = this
                    api.closeFrame()
                },

                //数组去重
                arraySet(array) {
                    var that = this
                    return Array.from(new Set(array))
                },

                //切换tab
                tabClick() {
                    var that = this
                    console.log(JSON.stringify(api.pageParam))
                    if (api.pageParam.equipment.EquipmentName.indexOf('涂油') == -1) { //普通注塑机
                        var obj = {
                            EquipmentID: api.pageParam.equipment.EquipmentID,
                            ISRepor: that.activeTab
                        }
                        console.log('obj' + JSON.stringify(obj))

                        APP.ajax('getZSAllPlan_FB', obj, function (res) {
                            console.log('res' + JSON.stringify(res))

                            that.tableData = res.ds
                            // var isAutoLine = api.pageParam.equipment.EquipmentID >= 3558 &&
                            //     api.pageParam.equipment.EquipmentID <= 3566;

                            // if (isAutoLine && that.activeTab === 1) { // 仅针对 "可报工" 选项
                            //     // 处理并排序数据
                            //     let filteredData = res.ds.map(item => {
                            //         var prodNo = item.ProductionNO;
                            //         var month = parseInt(prodNo.substring(4, 6)); // 提取月份
                            //         var tailNum = parseInt(prodNo.substring(6).replace(/^0+/, '')); // 提取尾数
                            //         return {
                            //             ...item,
                            //             month,
                            //             tailNum
                            //         };
                            //     }).sort((a, b) => {
                            //         // 按优先级排序：尾数 → 制单时间 → 已报工数
                            //         if (a.tailNum !== b.tailNum) {
                            //             return a.tailNum - b.tailNum; // 尾数小的优先
                            //         } else if (a.Billdate !== b.Billdate) {
                            //             return new Date(a.Billdate) - new Date(b.Billdate); // 制单时间早的优先
                            //         } else {
                            //             return b.ReporQTY - a.ReporQTY; // 已报工数多的优先
                            //         }
                            //     });

                            //     // 记录最优选的订单
                            //     if (filteredData.length > 0) {
                            //         that.bestPlan = filteredData[0]; // 存储最优选的订单
                            //     }

                            //     that.tableData = filteredData;
                            // }

                            if (res.ds.length > 0 && res.ds[0].ISRepor == '补') {
                                return
                            }
                            else {
                                if (api.pageParam.choosePlan && that.activeTab == 0) {
                                    if (res.ds.length) {
                                        that.planIDClick(res.ds[0])
                                    } else {
                                        that.activeTab = 1
                                        that.tabClick()
                                    }
                                }
                                else if (api.pageParam.choosePlan && that.activeTab == 1) {
                                    if (res.ds.length) {
                                        that.planIDClick(res.ds[0])
                                    }
                                }
                            }
                        })
                    } else {  //涂油机台
                        var obj = {
                            FBoxCode: that.inputCode,
                            EquipmentID: api.pageParam.equipment.EquipmentID,
                            ISRepor: that.activeTab
                        }
                        console.log('FBoxCode' + JSON.stringify(obj))
                        APP.ajax('getZSTYAllPlan', obj, function (res) {
                            console.log('FBoxCode' + JSON.stringify(res))
                            that.scanShow = false
                            if (res.ds[0].status == 500) {
                                that.dialogMsg = res.ds[0].msg
                                that.dialogShow = true
                            } else {
                                that.tableData = res.ds
                                if (that.inputCode) {
                                }
                                var TYobj = {
                                    FBoxCode: that.inputCode,
                                }
                                console.log('TYobj', JSON.stringify(TYobj))
                                APP.ajax('ZS_PrintAgain_TY', TYobj, function (res) {
                                    console.log('TYres', JSON.stringify(res.ds[0]))
                                    if (res.ds) {
                                        res.ds[0].FBoxCode = that.inputCode
                                        that.beforeMsg = res.ds[0]
                                    }
                                })
                            }

                        })
                    }
                },
                planIDClick(planIDitem) {
                    var that = this
                    console.log('planIDitem', JSON.stringify(planIDitem))
                    // if (api.pageParam.equipment.EquipmentID >= 3558 && api.pageParam.equipment.EquipmentID <= 3566) {
                    //     var isAutoLine = api.pageParam.equipment.EquipmentID >= 3558 &&
                    //         api.pageParam.equipment.EquipmentID <= 3566;
                    //     // 2. 如果是自动线且选择了 "可报工" 选项，校验优先级
                    //     if (isAutoLine && that.activeTab === 1 && that.bestPlan) {
                    //         var isBestPlan = planIDitem.ZhuSuPlanID === that.bestPlan.ZhuSuPlanID;

                    //         if (!isBestPlan) {
                    //             api.toast({
                    //                 msg: '有优先级更高的订单！',
                    //                 duration: 3000,
                    //                 location: 'middle'
                    //             });
                    //             return; // 阻止跳转
                    //         }
                    //     }
                    // } else {


                        if (planIDitem.WhetherStarteMold == '未上模') {
                            api.toast({
                                msg: '请先上模！'
                            })
                            return false
                        }
                        if (that.activeTab == 0 || that.activeTab == 1) {
                            api.sendEvent({
                                name: 'refreshProductReport'
                            })
                            api.sendEvent({
                                name: 'resetBreadList',
                                extra: {
                                    breadData: {
                                        name: '生产报工',
                                        url: 'productReport',
                                        ZS_FunctionID: 0,
                                        click: true
                                    }
                                }
                            })
                            var newRect
                            if (window.localStorage.getItem('clientHeight') != 752) {
                                newRect = {
                                    x: 0,
                                    y: 115,
                                    w: 'auto',
                                    h: api.winHeight - 150 - api.safeArea.bottom
                                }
                            } else {
                                newRect = {
                                    x: 0,
                                    y: 170,
                                    w: 'auto',
                                    h: api.winHeight - 230 - api.safeArea.bottom
                                }
                            }
                            api.openFrame({
                                name: 'productReport',
                                url: 'productReport.html',
                                rect: newRect,
                                bgColor: '#29292a',
                                reload: true,
                                pageParam: {
                                    ZhuSuPlanID: planIDitem.ZhuSuPlanID,
                                    equipment: api.pageParam.equipment,
                                    classOption: api.pageParam.classOption,
                                    ZS_ZhuSuReporID: planIDitem.ZS_ZhuSuReporID,
                                    roleName: api.pageParam.roleName,
                                    currentBatch: api.pageParam.currentBatch,
                                    beforeMsg: that.beforeMsg
                                },
                            })
                        }
                    }
                }
            // }
        })
    };
</script>

</html>